(function () {
    Codestrong.ui.CreateCategoryDownloadDetail= function (_args) {

//var LectureBySubject = function() {
	var aps='';
	var mobilier = [];
	if(Ti.Platform.osname== 'android') {
  		 Titanium.include('../globals.js');
  		 aps='../';
    }
    else {
   		  Titanium.include('globals.js');
    }
	
	
	
	
	
	/////////////////////FUNCTION//////////
	var getTableData_ListVideo = function(LectureId) {
		var db = require('db');
		var data = [];
		var row = null;
		var Items = db.selectLectureVideoDownload(LectureId);
		//alert(LectureId)
		for (var i = 0; i < Items.length; i++) {
			mobilier[i] =Items[i];
		}
		
		//alert(mobilier)
		
		
		PopularDataVideo(mobilier)
		Codestrong.ui.activityIndicator.hideModal();
		
		
	};
	
	
	
	
	
	
	
	var win = Titanium.UI.createWindow({
            id: 'CategoryDownloadDetail',
           // title: _args.TenMonHoc,//_args.LectureName,
            backgroundColor: '#FFF',
            barColor: '#414444',
            fullscreen: false
    });
    var egc = new globals.EventGarbageCollection(win);

  
   
   
    egc.addEventListener('back_CategoryDownloadDetail', function () {
 		
    	
 		Codestrong.navGroup.close(win, {
                        animated: true
        });
        
	});
	

	egc.addEventListener('CategoryDownloadDetail:playvideoinTable', function (param) {
		var filename=param.dataVideo.VideoFile+'.mp4'
		
		
		if(Codestrong.isAndroid()) 
		{
			f = Ti.Filesystem.getFile(Titanium.Filesystem.getExternalStorageDirectory(),filename);
		
		}
		else 	
		{
		    f = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory+filename);
		}
		
		var VideoUrl=f.nativePath;
		
		
		//alert(urlVideo)
			spPlayVideo(VideoUrl)
	});
	




	
	spPlayVideo= function(urlvideo)
	{
		Codestrong.navGroup.open(Codestrong.ui.createVideoPlayWindow({
								        VideoUrl:  urlvideo}), 
									    { 
									        animated: true,
		    								navBarHidden:true
									    });	
	}
	
	
	
	
	
	
	////////////main_header///////////////////////////
	 var databutton_right=[
    	//{Id:1,Image:aps+'images/list_bullets.png',Image_:aps+'images/list_bullets_.png',tag:'Category'},
	    {Id:2,Image:aps+'images/account.png',Image_:aps+'images/account_.png',tag:'Login'},
	   // {Id:3,Image:aps+'images/search.png',Image_:aps+'images/search_.png',tag:'Search'},
	    
	]
	
	
	var main_header=require('controls/main_header').main_header({
	 		backgroundImage: aps+'images/panel3.png',
	 		headertext: 'Thầy trò Mobile: Bài giảng đã tải',
		    left: 0,
		    top: 0,
		    height:65,
		    width: Globals.platformWidth,
		    touchEnabled: true		,
		    databutton_right:databutton_right,
		   
		    imback:   aps+'images/back.png',
		    imback_:   aps+'images/back_.png',
		    idWinParent:'CategoryDownloadDetail',
    })

	win.add(main_header);
	
	////////////LectureDetail_header///////////////////////////
	
	
	var LectureDetail_header=require('controls/CategoryDownloadDetail_header').CategoryDownloadDetail_header({
	 		backgroundImage: aps+'images/sessionbckgd.png',
	 		
		    left: 0,
		    top: main_header.top+main_header.height,
		    height:155,
		    width: Globals.platformWidth,
		    touchEnabled: true		,
		    backgroundImage_ImageTitle:aps+'images/teacher/'+_args.TeacherImage,
		    lbLectureName:_args.LectureName,
		 	lbPrice:_args.Price,
		 	btheight_button:45,
		 	
		 	LectureId:_args.LectureId
		    
    })

	win.add(LectureDetail_header);
	
	
	
	var ScrollViewMain = Ti.UI.createScrollView({
		top:LectureDetail_header.top+LectureDetail_header.height,
		
		touchEnabled:true,
		//height:'100%',
		scrollsToTop:true,
		
	});
	
	
	
	//////////////////////////////////////////////////////
	var PopularDataVideo = function(dataVideo) {
		
		var Items=dataVideo,countitemt=0,rowindex=0;
		var top_t=0
		var IsMod=0
		var lengthData=Items.length
		
	    switch (lengthData) {
		          case 0: {countrow=0;}
		          break;
		          case 1: {countrow=1;}
		          break;
		          case 2: {countrow=1;}
		          break;
		          case 3: {countrow=1;}
		          break;
		          case 4: {countrow=1;}
		          break;
		          case 5: {countrow=2;}
		          break;
		          case 6: {countrow=2;}
		          break;
		          case 7: {countrow=2;}
		          break;
		          case 8: {countrow=2;}
		          break;
		          case 9: {countrow=3;}
		          break;
		          case 10: {countrow=3;}
		          break;
		          case 11: {countrow=3;}
		          break;
		          case 12: {countrow=3;}
		          break;
		          case 13: {countrow=4;}
		          break;
		          case 14: {countrow=4;}
		          break;
		          case 15: {countrow=4;}
		          break;
		          case 16: {countrow=4;}
		          break;
		 
		          default:
		          break;
		      }
		
		

	
	   var vi = Ti.UI.createView({
				height:'auto',//countrow*Globals.platformWidth/4,
				width:Globals.platformWidth,
				top:0,
				backgroundImage:aps+'images/Dark.png',
				borderRadius:0,
				borderColor:'black',
				borderWidth:1,
				touchEnabled:true,
		});
		
		ScrollViewMain.add(vi);
	
		
	//	alert('Leng'+lengthData+' Nguyen thuy'+lengthData/4+' Tron:'+Math.round((lengthData/4))) 
	
		for(var i = 1; i <= countrow; i++) {
			if(lengthData-4>0)
			{
				 countitemt = 4
				 lengthData-= 4
			}
			else
			{
				 countitemt = lengthData
			}
				
			var LectureDetail_tablerow=require('controls/CategoryDownloadDetail_tablerow').CategoryDownloadDetail_tablerow({
		 			top:top_t,
		  			height:Globals.platformWidth/4,
		  			width:Globals.platformWidth,
					backgroundImage_Child:aps+'images/video-player.png',
					backgroundColorSection:'#2981a3',
					
					countitemt:countitemt,
					rowindex:i-1,
					IsDownload:_args.IsDownload,
					IsSee:_args.IsSee,
					LectureId:_args.LectureId,
					dataVideo:dataVideo
				
			    })
		   vi.add(LectureDetail_tablerow);
		   top_t+=Globals.platformWidth/4;
		}
		 
		ScrollViewMain.add(vi);
		
		
		
		
	
		
		
		
		
	}
	
	
	
	win.add(ScrollViewMain);
	/////////////////////////OPEN WINDOWN/////////////////////////
	
	Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
	getTableData_ListVideo(_args.LectureId)
	
	///////////////////////////////////////////////////////////////
 	return win;
       
    };
})();